//
//  XQRouter.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/30.
//

#import "XQRouter.h"
#import "XQAuthenticationController.h"
#import "XQNavigationController.h"
#import "XQHomeTabBarController.h"
#import "XQUserSettingController.h"
#import "XQLoginViewController.h"

@implementation XQRouter

+ (void)jumpFromController:(UIViewController *)controller url:(NSString *)url {
    if ([url hasPrefix:@"http"]) {
        XQWebViewController *vc = [[XQWebViewController alloc] init];
        vc.url = [XQNetManager getWebUrlAbsolutePath:url];
        if ([controller isKindOfClass:[XQNavigationController class]]) {
            [(XQNavigationController *)controller pushViewController:vc animated:YES];
        }else {
            [controller.navigationController pushViewController:vc animated:YES];
        }
    }else {
        if ([url containsString:@"jo://i.npe.so/use"]) {
            NSArray *arr = [url componentsSeparatedByString:@"?"];
            if (arr.count == 2) {
                NSString *accommodate = arr.lastObject;
                NSArray *valueArr = [accommodate componentsSeparatedByString:@"="];
                if (valueArr.count == 2) {
                    NSString *cardId = valueArr.lastObject;
                    if (cardId.length) {
                        [self jumpProductDetailFromController:controller cardId:cardId];
                    }
                }
            }
        }else if ([url containsString:@"jo://i.npe.so/raised"]) {
            XQHomeTabBarController *tabController = [self findTabController];
            [tabController goOrderListController];            
        }else if ([url containsString:@"jo://i.npe.so/top"]) {
            XQLoginViewController *vc = [XQLoginViewController new];
            if ([controller isKindOfClass:[XQNavigationController class]]) {
                [(XQNavigationController *)controller pushViewController:vc animated:YES];
            }else {
                [controller.navigationController pushViewController:vc animated:YES];
            }
        }else if ([url containsString:@"jo://i.npe.so/united"]) { // 首页
            XQHomeTabBarController *tabController = [self findTabController];
            [tabController goHomeViewController];            
        }else if ([url containsString:@"jo://i.npe.so/from"]) {
            XQUserSettingController *vc = [XQUserSettingController new];
            if ([controller isKindOfClass:[XQNavigationController class]]) {
                [(XQNavigationController *)controller pushViewController:vc animated:YES];
            }else {
                [controller.navigationController pushViewController:vc animated:YES];
            }
        }
    }
}

+ (void)jumpProductDetailFromController:(UIViewController *)controller cardId:(NSString *)cardId {
    XQAuthenticationController *vc = [XQAuthenticationController new];
    vc.cardId = cardId;
    vc.backHandle = ^(NSString * _Nonnull url,BOOL needCloseCurrentPage) {
        
        XQWebViewController *vc = [XQWebViewController new];
        vc.url = [XQNetManager getWebUrlAbsolutePath:url];
        
        UINavigationController *navController;
        
        if ([controller isKindOfClass:[XQNavigationController class]]) {
            navController = (XQNavigationController *)controller;
        }else {
            navController = controller.navigationController;
        }
        
        NSMutableArray *viewControllers = [navController.viewControllers mutableCopy];
        [viewControllers removeLastObject];
        [viewControllers addObject:vc];
        [navController setViewControllers:viewControllers animated:YES];
        

    };
    if ([controller isKindOfClass:[XQNavigationController class]]) {
        [(XQNavigationController *)controller pushViewController:vc animated:YES];
    }else {
        [controller.navigationController pushViewController:vc animated:YES];
    }
}


+ (XQHomeTabBarController *)findTabController {
    UINavigationController *rootNav = (UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    if (![rootNav isKindOfClass:[UINavigationController class]]) {
        return nil;
    }
    XQHomeTabBarController *homeTabController = rootNav.viewControllers.firstObject;
    if (![homeTabController isKindOfClass:[XQHomeTabBarController class]]) {
        return nil;
    }
    return homeTabController;
}

+ (void)jumpToHome {
    XQHomeTabBarController *tabController = [self findTabController];
    [tabController goHomeViewController];     
}

@end
